package com.baosf.serviceorder.controller;

import com.baosf.model.order.Order;
import com.baosf.serviceorder.config.OrderProperties;
import com.baosf.serviceorder.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//1. @RefreshScope + @Value 自动刷新
//@RefreshScope
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;
//    实现微服务读取的配置内容的动态刷新，我们有 3 种方式：
//            1. @RefreshScope + @Value 自动刷新
//            2. @ConfigurationProperties 无感自动刷新
//            3. NacosConfigManager 监听配置变化
//1. @RefreshScope + @Value 自动刷新
//    @Value("${order.timeout}")
//    private String orderTimeout;
//
//    @Value("${order.auto-confirm}")
//    private String orderAutoConfirm;
//2. @ConfigurationProperties 无感自动刷新
    @Resource
    private OrderProperties orderProperties;

    @GetMapping("/order/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId) {
        return orderService.createOrder(userId, productId);
    }
//    1.@RefreshScope + @Value 自动刷新
//    @GetMapping("/order/config")
//    public String getConfig() {
//        return "orderTimeout=" + orderTimeout + ", orderAutoConfirm=" + orderAutoConfirm;
//    }
//    2.@ConfigurationProperties 无感自动刷新
    @GetMapping("/order/getConfigConfigurationProperties")
    public String getConfigConfigurationProperties() {
        return "orderTimeout=" + orderProperties.getTimeout() + ", orderAutoConfirm=" + orderProperties.getAutoConfirm();
    }
}
